package org.qydjk.console.controller.basic;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.*;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.ext.SpecialLocEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/specialLoc")
public class SpecialLocController extends BaseController{
    @RequestMapping("/index.do")
    public String index(){
        return "basic/specialLoc/specialLoc";
    }

    @RequestMapping("/list.do")
    @ResponseBody
    public Object list(Integer type,Integer platform,String name){
        try {
            StringBuffer sb = new StringBuffer("from SpecialLoc loc where 1 = 1");
            StringBuffer sbCt = new StringBuffer("select count(loc.id) from SpecialLoc loc where 1 = 1");
            if(type != null){
                sb.append(" and loc.type = " + type.intValue());
                sbCt.append(" and loc.type = " + type.intValue());
            }
            if(platform != null && platform.intValue() != -1){
                sb.append(" and loc.platform = " + platform.intValue());
                sbCt.append(" and loc.platform = " + platform.intValue());
            }
            if(!StringUtil.isEmptyOrNull(name)){
                sb.append(" and loc.name like '%" + name + "%'");
                sbCt.append(" and loc.name like '%" + name + "%'");
            }
            sb.append(" order by loc.id desc");
            List<SpecialLoc> list = baseService.findByHql(sb.toString());
            int ct = baseService.getRecordCountHQL(sbCt.toString());
            return new EasyUIResponse<SpecialLoc>(ct,list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new EasyUIResponse<SpecialLoc>(0,new ArrayList<SpecialLoc>());
    }

    @RequestMapping("/add.do")
    @ResponseBody
    public Object add(SpecialLoc loc){
        try {
            loc.setCreateTime(new Date());
            loc.setUpdateTime(new Date());
            baseService.save(loc);
            return renderSuccess("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("新增失败");
        }
    }

    @RequestMapping("/update.do")
    @ResponseBody
    public Object update(SpecialLoc loc){
        try {
            SpecialLoc dbLoc = baseService.get(SpecialLoc.class, loc.getId());
            BeanUtil.copyProperties(loc,dbLoc);
            dbLoc.setUpdateTime(new Date());
            baseService.update(dbLoc);
            return renderSuccess("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("修改失败");
        }
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public Object delete(int id){
        try {
            SpecialLoc loc = baseService.get(SpecialLoc.class,id);
            String ctHQL = "select count(sd.id) from SpecialDetail sd where sd.specialLocId = " + id;
            if(loc.getType() == SpecialLocEnum.SELLER.getType()){
                ctHQL = "select count(sd.id) from SpecialLocSeller sd where sd.locId = " + id;
            }else if(loc.getType() == SpecialLocEnum.PRODUCT.getType()){
                ctHQL = "select count(sd.id) from SpecialLocProduct sd where sd.locId = " + id;
            }else if(loc.getType() == SpecialLocEnum.ARTICLE.getType()){
                ctHQL = "select count(sd.id) from SpecialLocArticle sd where sd.locId = " + id;
            }
            int size = baseService.getRecordCountHQL(ctHQL);
            if(size > 0){
                return renderError("请先删除子项！");
            }else{
                baseService.delete(loc);
                return renderSuccess("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
}
